home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / BCCollec / Support / BCHashTa.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  5.0 KB  |  171 lines  |  [TEXT/MPS ]

  1. //  The C++ Booch Components (Version 2.1)
  2. //  (C) Copyright 1990-1993 Grady Booch. All Rights Reserved.
  3. //
  4. //  Restricted Rights Legend
  5. //  Use, duplication, or disclosure is subject to restrictions as set forth 
  6. //  in subdivision (c)(1)(ii) of the Rights in Technical Data and Computer 
  7. //  Software clause at DFARS 252.227-7013. 
  8. //
  9. //  BCHashTa.cpp
  10. //
  11. //  This file contains the definition for the open hash table class.
  12.  
  13. #include "BCHashTa.h"
  14.  
  15. template<class Item, class Value, BC_Index Buckets, class Container>
  16. BC_TTable<Item, Value, Buckets, Container>::
  17.   BC_TTable(const BC_TTable<Item, Value, Buckets, Container>& t)
  18.     : fValidCache(0),
  19.       fSize(0),
  20.       fHash(t.fHash)
  21. {
  22.   operator=(t);
  23. }
  24.  
  25. template<class Item, class Value, BC_Index Buckets, class Container>
  26. BC_TTable<Item, Value, Buckets, Container>&
  27.   BC_TTable<Item, Value, Buckets, Container>::
  28.     operator=(const BC_TTable<Item, Value, Buckets, Container>& t)
  29. {
  30.   if (this == &t)
  31.     return *this;
  32.   else {
  33.     Clear();
  34.     for (BC_Index bucket_index = 0; (bucket_index < Buckets); bucket_index++)
  35.       for (BC_Index index = 0; (index < t.fRep[bucket_index].Length()); index++) {
  36.         BC_Index bucket = fHash(t.fRep[bucket_index][index].fItem) % Buckets;
  37.         fRep[bucket].Append(t.fRep[bucket_index][index]);
  38.       }
  39.     fSize = t.fSize;
  40.     return *this;
  41.   }
  42. }
  43.  
  44. template<class Item, class Value, BC_Index Buckets, class Container>
  45. BC_Boolean BC_TTable<Item, Value, Buckets, Container>::
  46.   operator==(const BC_TTable<Item, Value, Buckets, Container>& t) const
  47. {
  48.   if (this == &t)
  49.     return 1;
  50.   else {
  51.     if (fSize == t.fSize) {
  52.       for (BC_Index bucket = 0; (bucket < Buckets); bucket++)
  53.         for (BC_Index index = 0; (index < fRep[bucket].Length()); index++) {
  54.           const BC_TPair<Item, Value>* pair = 
  55.             &fRep[bucket].ItemAt(index);
  56.           if (!t.IsBound(pair->fItem) ||
  57.             (pair->fValue != *(t.ValueOf(pair->fItem))))
  58.             return 0;
  59.          }
  60.       return 1;
  61.     }
  62.     return 0;
  63.   }
  64. }
  65.  
  66. template<class Item, class Value, BC_Index Buckets, class Container>
  67. void BC_TTable<Item, Value, Buckets, Container>::Clear()
  68. {
  69.   for (BC_Index index = 0; (index < Buckets); index++)
  70.     fRep[index].Clear();
  71.   fValidCache = 0;
  72.   fSize = 0;
  73. }
  74.  
  75. template<class Item, class Value, BC_Index Buckets, class Container>
  76. BC_Boolean BC_TTable<Item, Value, Buckets, Container>::
  77.   Bind(const Item& item, const Value& value)
  78. {
  79.   if (fValidCache && (fCache.fItem == item))
  80.     return 0;
  81.   else {
  82.     BC_Index bucket = fHash(item) % Buckets;
  83.     BC_ExtendedIndex index =
  84.       fRep[bucket].Location((BC_TPair<Item, Value>)item);
  85.     if (index != -1)
  86.       return 0;
  87.     else {
  88.       fCache = BC_TPair<Item, Value>(item, value);
  89.       fValidCache = 1;
  90.       fRep[bucket].Insert(fCache);
  91.       fSize++;
  92.     }
  93.     return 1;
  94.   }
  95. }
  96.  
  97. template<class Item, class Value, BC_Index Buckets, class Container>
  98. BC_Boolean BC_TTable<Item, Value, Buckets, Container>::
  99.   Rebind(const Item& item, const Value& value)
  100. {
  101.   BC_Index bucket = fHash(item) % Buckets;
  102.   BC_ExtendedIndex index =
  103.     fRep[bucket].Location((BC_TPair<Item, Value>)item);
  104.   if (index != -1) {
  105.     fCache = BC_TPair<Item, Value>(item, value);
  106.     fValidCache = 1;
  107.     BC_TPair<Item, Value>* pair = &fRep[bucket].ItemAt(index);
  108.     pair->fValue = value;
  109.     return 1;
  110.    }
  111.    return 0;
  112. }
  113.  
  114. template<class Item, class Value, BC_Index Buckets, class Container>
  115. BC_Boolean BC_TTable<Item, Value, Buckets, Container>::Unbind(const Item& item)
  116. {
  117.   BC_Index bucket = fHash(item) % Buckets;
  118.   BC_ExtendedIndex index = fRep[bucket].Location(item);
  119.   if (index != -1) {
  120.     if (fValidCache && (fCache.fItem == item))
  121.       fValidCache = 0;
  122.     fRep[bucket].Remove(index);
  123.     fSize--;
  124.     return 1;
  125.   }
  126.   return 0;
  127. }
  128.  
  129. template<class Item, class Value, BC_Index Buckets, class Container>
  130. BC_Boolean BC_TTable<Item, Value, Buckets, Container>::
  131.   IsBound(const Item& item) const
  132. {
  133.   if (fValidCache && (fCache.fItem == item))
  134.     return 1;
  135.   else {
  136.     BC_Index bucket = 
  137.       ((BC_TTable<Item, Value, Buckets, Container>&)*this).
  138.         fHash(item) % Buckets;
  139.     BC_ExtendedIndex index = fRep[bucket].Location(item);
  140.     if (index != -1) {
  141.       ((BC_TTable<Item, Value, Buckets, Container>&)*this).fCache = 
  142.         fRep[bucket].ItemAt(index);
  143.       ((BC_TTable<Item, Value, Buckets, Container>&)*this).
  144.         fValidCache = 1;
  145.       return 1;
  146.     }
  147.     return 0;
  148.   }
  149. }
  150.  
  151. template<class Item, class Value, BC_Index Buckets, class Container>
  152. const Value* BC_TTable<Item, Value,Buckets, Container>::ValueOf(const Item& item) const
  153. {
  154.   if (fValidCache && (fCache.fItem == item))
  155.     return &fCache.fValue;
  156.   else {
  157.     BC_Index bucket = 
  158.       ((BC_TTable<Item, Value, Buckets, Container>&)*this).
  159.         fHash(item) % Buckets;
  160.     BC_ExtendedIndex index = fRep[bucket].Location(item);
  161.     if (index != -1) {
  162.       ((BC_TTable<Item, Value, Buckets, Container>&)*this).fCache =
  163.         fRep[bucket].ItemAt(index);
  164.       ((BC_TTable<Item, Value, Buckets, Container>&)*this).
  165.         fValidCache = 1;
  166.       return &fRep[bucket][index].fValue;
  167.     }
  168.     return 0;
  169.   }
  170. }
  171.